import 'package:flutter/material.dart';

// 分组里每一项的数据格式
class ServiceItem {
  final String name;
  final String imgUrl;
  final String route;

  ServiceItem({
    required this.name,
    required this.imgUrl,
    required this.route,
  });
}

// 服务页的分组入口组件
class ServiceGroupWidget extends StatelessWidget {
  final double padding;
  final String group;
  final List<ServiceItem> childrenList;
  const ServiceGroupWidget({
    super.key,
    required this.padding, // 外侧的左右间距，用来计算每个盒子的宽度
    required this.group,
    required this.childrenList,
  });

  @override
  Widget build(BuildContext context) {

    const spacing = 12.0;
    const runSpacing = 8.0;

    // 每一项Widget
    Widget itemWidget(ServiceItem item) {
      return InkWell(
        onTap: () {
          Navigator.pushNamed(context, 'EmptyPage', arguments: {
            'title': item.name,
          });
        },
        child: Container(
          padding: const EdgeInsets.only(top: 18),
          width: (MediaQuery.of(context).size.width - 24 - padding * 2 - 3 * spacing) / 4,
          child: Column(
            children: [
              Image.asset(item.imgUrl, width: 32),
              const SizedBox(height: 6),
              Text(item.name, style: const TextStyle(fontSize: 12),),
            ],
          ),
        ),
      );
    }

    return Container(
      padding: const EdgeInsets.only(top: 12),
      child: Container(
        width: double.infinity,
        padding: const EdgeInsets.fromLTRB(12, 12, 12, 30),
        decoration: const BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.all(Radius.circular(8)),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(group, style: const TextStyle(fontSize: 14, color: Colors.grey),),
            const SizedBox(height: 6),
            SizedBox(
              width: double.infinity,
              child: Wrap(
                spacing: spacing,
                runSpacing: runSpacing,
                children: childrenList.map((item) => itemWidget(item)).toList(),
              ),
            )
          ],
        ),
      ),
    );
  }
}
